package com.luchao.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.Random;

/**
 * 成语接龙，输入一个词，查询出10个
 */
public class HtmlParseUtilCY {
    private static Integer num = 10;

    public static void main(String[] args) throws IOException {
        String name = "一步登天";
        cyjl(name, "", 0);
    }

    private static void cyjl(String name, String shangtwo, Integer iii) throws IOException {
        String url = "https://www.chengyujielong.com.cn/detail/" + name;
        Document document = Jsoup.parse(new URL(url), 30000);
        Elements element = document.getElementsByClass("col-xs-4");
        if (element.size() > 0) {
//            System.out.println("el:" + element.get(1));
            String str = element.get(1).text();
            //        System.out.println(str);
            if (str.equals("")) {
                if (iii < num) {
                    iii++;
                    cyjl(shangtwo, shangtwo, iii);
                }
            } else {
                String[] sss = str.split(" ");
                int n = new Random().nextInt(sss.length);
                System.out.println(sss[n]);
//                System.out.println(sss[n] + " >>>>>>  " + str);
                if (sss.length > 1) {
                    shangtwo = sss[1];
                } else {
                    shangtwo = sss[0];
                }
//            System.out.println(sss[0]);
                if (iii < num) {
                    iii++;
                    cyjl(sss[n], shangtwo, iii);
                }
            }


        } else {
            System.out.println("不是成语吧！");
        }
    }
}
